<?php
require_once 'Abstracts.php';

class SystemModel extends Abstracts{

    public function getConf($sql){
        $info = array();
        $data = $this->getAll($sql);
        foreach($data as $v ){
            $info[$v['name']] = $v['value'];
        }
        return $info;
    }
    
    public function sqlquery($sql){
        return $this->query($sql);
    }
    
    public function roomCount($where){

        $sql = " SELECT count(id) AS cnc FROM pk_room ".$where;
        $res = $this->getRow($sql);
        return $res['cnc']?$res['cnc']:0;
    }
    
    public function gradeCount(){
        $sql = " SELECT count(id) AS cnc FROM pk_system_grade ";
        $res = $this->getRow($sql);
        return $res['cnc']?$res['cnc']:0;
    }
    
    public function AddRoomToGame(){
        $roomSql = " SELECT id FROM pk_room WHERE isDel = '0'" ;
        $roomRes = $this->getAll($roomSql);
        
        foreach ( $roomRes as $v ){
            $roomArr[] = $v['id'];
        }
        $gameSql = " SELECT roomID FROM pk_game ";
        $gameRes = $this->getAll($gameSql);
       
        foreach( $gameRes as $v ){
            $gameArr[] = $v['roomID'];
        }
        foreach ( $roomArr as $v ){
            if(!in_array($v, $gameArr)){
                $addRoom[] = $v;
            }
        }
        
        if( 0 < count($addRoom) ){
            foreach ( $addRoom as $v ){
                $sql[] = "('{$v}')";
            }
            $sql = " INSERT INTO pk_game (`roomID`) VALUES".implode(",", $sql);
            $this->query($sql);
        }
        
        
        
    }
}